# -*- coding: utf-8 -*-
# @Time : 2022/7/12 11:07
# @Author : Dftre
# @Email : 3066417822@qq.com
# @File : processor.py
# @Project : 代码生成器


import re


class Processor:
    def __init__(self, rule: str = None, r: re.Pattern = None):
        self.rule = rule
        self.r = r
        if not self.r:
            self.r = re.compile(rule)
        if not self.rule:
            self.rule = self.r.pattern
        self._doc = {
            'role': self.get_rule()
        }
        pass

    def sub(self, text: str) -> str:
        return text

    def get_rule(self) -> str:
        return self.rule

    def _add_doc(self, key, val):
        self._doc[key] = val

    def __str__(self):
        doc = str(type(self))[:-1] + ' '
        for i in self._doc:
            doc += "{0}='{1}' ".format(i, self._doc[i])
        doc += '>'
        return doc


class GeneratorException(Exception):

    def __init__(self, ErrorProcessor, msg=''):
        self.msg = '{}处理器错误:{}'.format(ErrorProcessor, msg)

    def __str__(self):
        return self.msg
